package com.test.demo.controller;


import com.test.demo.commons.ResponseResult;
import com.test.demo.domain.Messages;
import com.test.demo.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *前台文章内容管理。
 *
 * @author 远 chards_
 * @date: 2020-02-22 12:06
 */
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials = "true")
@RestController
@RequestMapping("message")
public class MessageFrontController {

    @Autowired
    public MessagesService messagesService;
    /**
     * 主页显示最新文章内容
     *
     * @return
     */
    @GetMapping("show/all")
    public ResponseResult<List> all(){
        //学院新闻
        List<Messages> schoolnews = messagesService.selectByType(0,0,5);


        //通知公告
        List<Messages> notices = messagesService.selectByType(1,0,5);


        //学术动态
        List<Messages> trends = messagesService.selectByType(2,0,5);


        //学生风采
        List<Messages> studentstyles= messagesService.selectByType(3,0,5);


        List<Map<String ,Object>> messageFrontParamList=new ArrayList<>();
        Map<String ,Object>map =new HashMap<>();
        map.put("schoolnews",schoolnews);
        map.put("notices",notices);
        map.put("trends",trends);
        map.put("studentstyles",studentstyles);
        messageFrontParamList.add(map);
        return new ResponseResult<List>(ResponseResult.CodeStatus.OK,"主页文章内容",messageFrontParamList);
    }

    @GetMapping("show/detail")
    public ResponseResult<Messages> selectById(@RequestParam(value = "id",required = false,defaultValue = "0") int  id){
        if (id==0){
            return new ResponseResult<Messages>(ResponseResult.CodeStatus.FAIL,"传入的id不正确");
        }
        Messages messages = messagesService.selectById(id);
        if (messages==null){
            return new ResponseResult<Messages>(ResponseResult.CodeStatus.FAIL,"获取详细信息失败，不存在此id");
        }
        return new ResponseResult<Messages>(ResponseResult.CodeStatus.OK,"文章详细内容",messages);
    }

    @GetMapping("show/list")
    public ResponseResult<List> selectBytype(@RequestParam(value = "type",required = false,defaultValue = "-1") int  type){
        if (type==-1){
            return new ResponseResult<List>(ResponseResult.CodeStatus.FAIL,"传入的type为空");
        }
        List messageList = messagesService.selectByType(type,0,10);
        if (messageList==null){
            return new ResponseResult<List>(ResponseResult.CodeStatus.FAIL,"获取消息失败,请检查传入的type是否正确");
        }
        return new ResponseResult<List>(ResponseResult.CodeStatus.OK,"获取某一类型更多内容",messageList);
    }


}
